1
|
|
|
import { Map, List, fromJS } from 'immutable'; |
|
|
|
|
2
|
|
|
import { camelize } from './camelize'; |
3
|
|
|
|
4
|
|
|
export const getData = ( |
5
|
|
|
row = new Map(), columns = {}, colIndex = 0, editorValues = {} |
6
|
|
|
) => { |
7
|
|
|
|
8
|
|
|
if (!row.get) { |
9
|
|
|
row = fromJS(row); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
const column = columns[colIndex]; |
13
|
|
|
|
14
|
|
|
if (!column) { |
15
|
|
|
return undefined; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
const dataIndex = column.dataIndex || null; |
19
|
|
|
|
20
|
|
|
if (!dataIndex) { |
21
|
|
|
throw new Error('No dataIndex found on column', column); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (editorValues |
25
|
|
|
&& editorValues.get |
26
|
|
|
&& editorValues.get(dataIndex) !== undefined) { |
27
|
|
|
return editorValues.get(dataIndex); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (typeof dataIndex === 'string') { |
31
|
|
|
const val = row |
32
|
|
|
&& row.get |
33
|
|
|
&& row.get(dataIndex) !== undefined |
34
|
|
|
? row.get(dataIndex) |
35
|
|
|
: null; |
36
|
|
|
|
37
|
|
|
return val && val.toJS ? val.toJS() : val; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
else if (Array.isArray(dataIndex)) { |
|
|
|
|
41
|
|
|
const val = getValueFromDataIndexArr(row, dataIndex); |
42
|
|
|
return val && val.toJS ? val.toJS() : val; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
export const setKeysInData = (data) => { |
48
|
|
|
|
49
|
|
|
if (List.isList(data)) { |
50
|
|
|
|
51
|
|
|
if (data.getIn([0, '_key'])) { |
52
|
|
|
return data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return data.map((item, i) => item.set('_key', `row-${i}`)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!data || !Array.isArray(data)) { |
59
|
|
|
return List([]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (data[0] && data[0]._key === undefined) { |
63
|
|
|
data.forEach((row, i) => { |
64
|
|
|
row._key = `row-${i}`; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return fromJS(data); |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
export const getRowKey = (columns, rowValues, suffix) => { |
72
|
|
|
|
73
|
|
|
const uniqueCol = columns.filter(col => col.createKeyFrom); |
74
|
|
|
let val = rowValues.get('_key'); |
75
|
|
|
|
76
|
|
|
if (uniqueCol.length > 1) { |
77
|
|
|
throw new Error('Only one column can declare createKeyFrom'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (uniqueCol.length > 0) { |
81
|
|
|
const dataIndex = nameFromDataIndex(uniqueCol[0]); |
82
|
|
|
val = rowValues && rowValues.get(dataIndex) |
83
|
|
|
? rowValues.get(dataIndex) |
84
|
|
|
: rowValues.get('_key'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (suffix) { |
88
|
|
|
val = `${val}-${suffix}`; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return val; |
92
|
|
|
}; |
93
|
|
|
|
94
|
|
|
export const setDataAtDataIndex = (row, dataIndex, val) => { |
95
|
|
|
|
96
|
|
|
if (!row.toJS) { |
97
|
|
|
row = fromJS(row); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (typeof dataIndex === 'string') { |
101
|
|
|
return row.set(dataIndex, val); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (row.getIn(dataIndex)) { |
105
|
|
|
return row.setIn(dataIndex, val); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new Error('Invalid key path'); |
109
|
|
|
}; |
110
|
|
|
|
111
|
|
|
export const getValueFromDataIndexArr = (row, dataIndex) => { |
112
|
|
|
const val = row.getIn(dataIndex); |
113
|
|
|
|
114
|
|
|
return val !== undefined |
115
|
|
|
? val |
116
|
|
|
: ''; |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
export const nameFromDataIndex = (column) => { |
120
|
|
|
|
121
|
|
|
if (!column) { |
122
|
|
|
return ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (typeof column.dataIndex === 'string') { |
126
|
|
|
return column.dataIndex; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (Array.isArray(column.dataIndex)) { |
130
|
|
|
return column.dataIndex[column.dataIndex.length - 1]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (!column.dataIndex) { |
|
|
|
|
134
|
|
|
return camelize(column.name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
}; |
138
|
|
|
|